//
//  TJOrderRecardListCell.m
//  Teleport
//
//  Created by hc-101 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJOrderRecardListCell.h"

@interface TJOrderRecardListCell()

@property (weak, nonatomic) UILabel *buyCoinLabel;
@property (weak, nonatomic) UILabel *paymentLabel;
@property (weak, nonatomic) UILabel *timeValueLabel;
@property (weak, nonatomic) UILabel *totalAmounValueLabel;
@property (weak, nonatomic) UILabel *numberValueLabel;
@property (weak, nonatomic) UILabel *numberLabel;
@property (strong, nonatomic) NSDateFormatter *formatter;


@end

@implementation TJOrderRecardListCell

- (void)settingSubView {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _formatter = [[NSDateFormatter alloc] init];
    [_formatter setDateFormat:@"HH:mm MM/dd"];
    
    self.contentView.backgroundColor = UIColor_Hex(0xf2f2f2);
    
    UIView *tj_contentView = [UIView tj_addToView:self];
    tj_contentView.backgroundColor = UIColor.whiteColor;
    tj_contentView.layer.shadowOffset = CGSizeMake(0, 3);
    tj_contentView.backgroundColor = UIColor.whiteColor;
    tj_contentView.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.4].CGColor;
    tj_contentView.layer.shadowRadius = 5;
    tj_contentView.layer.shadowOpacity = 0.3;
    tj_contentView.layer.cornerRadius = 5;
    tj_contentView.layer.masksToBounds = NO;
    [tj_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.offset(16);
        make.right.offset(-16);
        make.bottom.offset(0);
    }];
    
    
    UILabel *buyCoinLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(16) color:UIColor_Hex(0x272E3F) title:@"".localizedString textAlignment:NSTextAlignmentLeft];
    [buyCoinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(16);
    }];
    
    UILabel *timeLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x666666) title:@"时间".localizedString textAlignment:NSTextAlignmentLeft];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.equalTo(buyCoinLabel.mas_bottom).offset(14);
    }];
    
    UILabel *timeValueLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(14) color:UIColor_Hex(0x666666) title:@"" textAlignment:NSTextAlignmentLeft];
    timeValueLabel.numberOfLines = 2;
    [timeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.width.mas_lessThanOrEqualTo(100);
        make.top.equalTo(timeLabel.mas_bottom).offset(6);
    }];
    
    
    UILabel *numberLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x666666) title:nil textAlignment:NSTextAlignmentLeft];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(timeLabel);
    }];
    
    UILabel *numberValueLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(14) color:UIColor_Hex(0x666666) title:@"" textAlignment:NSTextAlignmentLeft];
    [numberValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(timeValueLabel);
    }];
    
    UILabel *totalAmountLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x666666) title:@"交易总额(CNY)".localizedString textAlignment:NSTextAlignmentLeft];
    [totalAmountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-14);
        make.centerY.equalTo(numberLabel);
    }];
    
    UILabel *totalAmountValueLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(14) color:UIColor_Hex(0x666666) title:@"" textAlignment:NSTextAlignmentLeft];
    [totalAmountValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(totalAmountLabel);
        make.top.equalTo(numberValueLabel);
    }];
    
    UIView *lineView = [UIView tj_addToView:tj_contentView];
    lineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeLabel);
        make.right.equalTo(totalAmountLabel);
        make.height.offset(1);
        make.top.equalTo(timeValueLabel.mas_bottom).offset(10);
    }];
    
    UIView *bottomView = [UIView tj_addToView:tj_contentView];
    bottomView.backgroundColor = UIColor.clearColor;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(lineView.mas_bottom);
    }];
    
    UILabel *paymentLabel = [UILabel lableWithSuperView:bottomView font:UIFont_Px(13) color:UIColor_Hex(0x6D7278) title:@"" textAlignment:NSTextAlignmentCenter];
    [paymentLabel shearRoundedCornersWithRadiu:3];
    [paymentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(totalAmountLabel);
        make.width.offset(60);
        make.height.offset(30);
        make.centerY.offset(0);
    }];
    
    _buyCoinLabel = buyCoinLabel;
    _paymentLabel = paymentLabel;
    _timeValueLabel = timeValueLabel;
    _numberValueLabel = numberValueLabel;
    _numberLabel = numberLabel;
    _totalAmounValueLabel = totalAmountValueLabel;
}

- (void)setModel:(TJOrderRecordListModel *)model {
    _model = model;
    
//    NSString *buyCoinTitle = [model.userDirection isEqualToString:@"BUY"] ?  @"购买".localizedString : @"出售".localizedString;
    NSString *buyCoinTitle = @"";
    if ([model.userId isEqualToString:TJUserInfo.readInfo.userId]) {//自己
        buyCoinTitle = [model.userDirection isEqualToString:@"BUY"] ?  @"购买".localizedString : @"出售".localizedString;
    }else{//卖家
        buyCoinTitle = [model.storeDirection isEqualToString:@"BUY"] ?  @"购买".localizedString : @"出售".localizedString;
    }
    
    _buyCoinLabel.text = [NSString stringWithFormat:@"%@%@", buyCoinTitle, model.currency];
    _timeValueLabel.text = [self convertStrToTime:model.createTime];
    
    _numberLabel.text = [NSString stringWithFormat:@"数量(%@)".localizedString, model.currency];
    _numberValueLabel.text = model.num;
    _totalAmounValueLabel.text = [NSString stringWithFormat:@"￥%@", model.totalPrice];
    
    self.paymentLabel.textColor = UIColor.whiteColor;
    
    int state = 0;
    //1：已完成，2：已取消，3：等待买方付款，4：等待卖方确认，5：自动取消. 6纠纷中
    
    //申诉状态  NORMAL--没申诉 UNDERWAY--申诉中 BACK--申诉撤销  SUCCEED--申诉成功  DEFEATED--申诉失败
    if ([model.appealStatus isEqualToString:@"NORMAL"]) {//没申诉
        // NONPAYMENT--未付款 PAYMENT-- 已付款  --FINISH 完成 --CALLOFF 撤销
        if ([model.status isEqualToString:@"NONPAYMENT"]) {//未付款
            state = 3;
        }else if ([model.status isEqualToString:@"PAYMENT"]){//已付款
            
            state = 4;
        }else if ([model.status isEqualToString:@"FINISH"]){//完成
            state = 1;
        }else{//CALLOFF 撤销(取消)
            state = 2;
        }
        
    }else if ([model.appealStatus isEqualToString:@"UNDERWAY"]){//申诉中
        state = 6;
    }else if ([model.appealStatus isEqualToString:@"BACK"]){//申诉撤销
        state = 1;
    }else if ([model.appealStatus isEqualToString:@"SUCCEED"]){//申诉成功
        state = 1;
    }else{//申诉失败
        state = 1;
    }
    
    if (state == 1) {
        _paymentLabel.text = @"已完成".localizedString;
        _paymentLabel.backgroundColor = UIColor.tj_mainColor;
    } else if (state == 2) {
        _paymentLabel.text = @"已取消".localizedString;
        _paymentLabel.backgroundColor = UIColor_Hex(0xFF0505);
    } else if (state == 3) {
        _paymentLabel.text = @"待付款".localizedString;
        _paymentLabel.backgroundColor = UIColor_Hex(0x70ACFF);
    } else if (state == 4) {
        _paymentLabel.text = @"待放行".localizedString;
        _paymentLabel.backgroundColor = UIColor_Hex(0x02A242);
    } else if (state == 5) {
        _paymentLabel.text = @"自动取消".localizedString;
        _paymentLabel.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    } else if (state == 6) {
        _paymentLabel.text = @"纠纷中".localizedString;
        _paymentLabel.backgroundColor = UIColor_Hex(0xc83a33);
    } else {
        _paymentLabel.text = @"";
        _paymentLabel.backgroundColor = UIColor.whiteColor;
    }
    
    CGFloat paymentLabelW = [self.paymentLabel.text getStringSizeWithWidth:200 font:self.paymentLabel.font].width + 30;
    [self.paymentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(paymentLabelW);
    }];
    
    
}
- (NSString *)convertStrToTime:(NSString *)timeStr {
    long long time=[timeStr longLongValue]/1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
}
@end
